package com.bawei.module_mine.adapter

import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bawei.lib_common.base.BaseApplication
import com.bawei.module_mine.R
import com.bawei.module_mine.model.CarOrderModelItem
import com.bawei.module_mine.model.InspectionOrderModelItem
import com.bawei.module_mine.model.MineServiceModel
import com.bawei.module_mine.model.NewsModelItem
import com.bawei.module_mine.model.RepairOrderModelItem
import com.bawei.module_mine.model.VisitOrderModelItem

class AuditAllAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    companion object {
        const val TYPE_VISITOR = 2 // 车位审核
        const val TYPE_REPAIR = 3 // 来访审核
        const val TYPE_PATROL = 4 // 新闻审核
    }
    private val carAuditList= mutableListOf<CarOrderModelItem>()
    private val visitAuditList= mutableListOf<VisitOrderModelItem>()
    private val newsAuditList= mutableListOf<NewsModelItem>()
    fun setCarData(data:List<CarOrderModelItem>){
        carAuditList.clear()
        if (data.isNotEmpty()){
            carAuditList.add(data[0])
        }
        notifyDataSetChanged()
    }
    fun setVisitData(data:List<VisitOrderModelItem>){
        visitAuditList.clear()
        if (data.isNotEmpty()){
            visitAuditList.add(data[0])
        }
        notifyDataSetChanged()
    }
    fun setNewsData(data:List<NewsModelItem>){
        newsAuditList.clear()
        if (data.isNotEmpty()){
            newsAuditList.add(data[0])
        }
        notifyDataSetChanged()
    }
    override fun getItemViewType(position: Int): Int {
        return when(position){
            0 -> TYPE_VISITOR
            1 -> TYPE_REPAIR
            2 -> TYPE_PATROL
            else -> {
                return TYPE_VISITOR
            }
        }
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when(viewType){
            TYPE_VISITOR -> CarViewHolder(View.inflate(parent.context, R.layout.item_car_audit, null))
            TYPE_REPAIR -> VisitViewHolder(View.inflate(parent.context, R.layout.item_car_audit, null))
            TYPE_PATROL -> NewsViewHolder(View.inflate(parent.context, R.layout.item_car_audit, null))
            else -> throw IllegalArgumentException("未知的视图类型")
        }
    }

    override fun getItemCount(): Int {
        return carAuditList.size + visitAuditList.size + newsAuditList.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        return when(holder){
            is CarViewHolder -> {
                if (carAuditList.isNotEmpty() && position < carAuditList.size){
                    val carOrder = carAuditList[position]
                    when(carOrder.status){
                        0 -> holder.carState.text="待审核"
                        1 -> {
                            holder.carState.text="已通过"
                            holder.carYes.visibility = View.GONE
                            holder.carNo.visibility = View.GONE
                        }
                        -1 -> {
                            holder.carState.text="已拒绝"
                            holder.carYes.visibility = View.GONE
                            holder.carNo.visibility = View.GONE
                        }
                    }
                    when (carOrder.status) {
                        0 -> holder.carState.setBackgroundResource(R.drawable.shape_item_repair_one)
                        1 -> holder.carState.setBackgroundResource(R.drawable.shape_item_repair_there)
                        -1 -> holder.carState.setBackgroundResource(R.drawable.shape_item_repair_four)
                        else -> android.R.color.black // 默认颜色
                    }
//                 根据id设置颜色（使用 ContextCompat 兼容不同API）
                    val colorRes = when (carOrder.status) {
                        0 -> com.bawei.lib_common.R.color.color_yellow_dark
                        1 -> com.bawei.lib_common.R.color.color_40b223
                        -1 -> com.bawei.lib_common.R.color.color_examine_red
                        else -> android.R.color.black // 默认颜色
                    }
                    holder.carState.setTextColor(ContextCompat.getColor(BaseApplication.instance, colorRes))
                    holder.carName.text = "车位号: ${carOrder.userName} - ${carOrder.id}"
                    holder.carTime.text = "转让期限: ${carOrder.createTime.substring(0,10)} 08:50:30"
                    holder.carPlane.text = "小红 - ${carOrder.areaName}"
                    holder.carAudit.text = "车位转让审核"
                    holder.carPurpose.text = "转让原因: 临时外出"
                }else{
                    // 无数据时隐藏或显示默认值
                    holder.carName.text = ""
                    holder.carTime.text = ""
                }
            }
            is VisitViewHolder -> {
                // 来访申请：position=1，对应 visitOrderList[0]（因为 position=0是车位，position=1是来访）
                val visitPosition = position - 1 // 0 -> 0, 1 -> 0（当总条目为2时）
                if (visitAuditList.isNotEmpty() && visitPosition < visitAuditList.size) {
                    val visitOrder = visitAuditList[visitPosition]
                    val colorRes = when (visitOrder.visitStatus) {
                        "待审核" -> com.bawei.lib_common.R.color.color_yellow_dark
                        "已通过" -> com.bawei.lib_common.R.color.color_40b223
                        "已完成" -> com.bawei.lib_common.R.color.color_40b223
                        "驳回" -> com.bawei.lib_common.R.color.color_examine_red
                        else -> android.R.color.black // 默认颜色
                    }
                    when (visitOrder.visitStatus) {
                        "待审核" -> holder.visitState.setBackgroundResource(R.drawable.shape_item_repair_one)
                        "已通过" -> holder.visitState.setBackgroundResource(R.drawable.shape_item_repair_there)
                        "已完成" -> holder.visitState.setBackgroundResource(R.drawable.shape_item_repair_there)
                        "驳回" -> holder.visitState.setBackgroundResource(R.drawable.shape_item_repair_four)
                        else -> android.R.color.black // 默认颜色
                    }
                    when(visitOrder.visitStatus){
                        "已通过" -> {
                            holder.visitYes.visibility = View.GONE
                            holder.visitNo.visibility = View.GONE
                        }
                        "已完成" -> {
                            holder.visitYes.visibility = View.GONE
                            holder.visitNo.visibility = View.GONE
                        }
                        "驳回" -> {
                            holder.visitYes.visibility = View.GONE
                            holder.visitNo.visibility = View.GONE
                        }
                    }
                    holder.visitState.setTextColor(ContextCompat.getColor(BaseApplication.instance, colorRes))
                    holder.visitState.text = "${visitOrder.visitStatus}"
                    holder.visitName.text = "访客: ${visitOrder.visitorName}"
                    holder.visitTime.text = "来访时间: ${visitOrder.appointmentTime}"
                    holder.visitPurpose.text = "来访事由: ${visitOrder.purpose}"
                    holder.visitAudit.text =  "访客预约审核"
                    holder.visitPlane.text = "张先生 -A栋1单元${visitOrder.id}"
                } else {
                    holder.visitState.text = ""
                    holder.visitName.text = ""
                    holder.visitTime.text = ""
                }
            }
            is NewsViewHolder -> {
                // 报修申请：position=2，对应 repairOrderList[0]（总条目≥3时）
                val newsPosition = position - 2 // 0->0, 1->0, 2->0（总条目≥3）
                if (newsAuditList.isNotEmpty() && newsPosition < newsAuditList.size) {
                    val newsOrder = newsAuditList[newsPosition]
                    val colorRes = when (newsOrder.status) {
                        "待审核" -> com.bawei.lib_common.R.color.color_yellow_dark
                        "已拒绝"-> com.bawei.lib_common.R.color.color_c73830
                        "未阅读" -> com.bawei.lib_common.R.color.color_12aa00
                        else -> android.R.color.black // 默认颜色
                    }
                    when (newsOrder.status) {
                        "待审核" -> holder.newsState.setBackgroundResource(R.drawable.shape_item_repair_one)
                        "已拒绝" -> holder.newsState.setBackgroundResource(R.drawable.shape_item_repair_two)
                        "未阅读" -> holder.newsState.setBackgroundResource(R.drawable.shape_item_repair_there)
                        else -> android.R.color.black // 默认颜色
                    }
                    when(newsOrder.status){
                        "已拒绝" -> {
                            holder.newsYes.visibility = View.GONE
                            holder.newsNo.visibility = View.GONE
                        }
                        "未阅读" -> {
                            holder.newsYes.visibility = View.GONE
                            holder.newsNo.visibility = View.GONE
                        }
                    }
                    holder.newsState.setTextColor(ContextCompat.getColor(BaseApplication.instance, colorRes))
                    holder.newsState.text = "${newsOrder.status}"
                    holder.newsPlane.text = "物业管理处"
                    holder.newsTime.text = "发布时间: ${newsOrder.publishTime}"
                    holder.newsAudit.text = "园区公告审核"
                    holder.newsName.text = "标题: ${newsOrder.title}"
                    holder.newsPurpose.text = "内容: ${newsOrder.content}"
                } else {
                    holder.newsState.text = ""
                    holder.newsName.text = ""
                    holder.newsTime.text = ""
                }
            }
            else -> throw IllegalArgumentException("未知的视图类型")
        }
    }
    class CarViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val carState: TextView = view.findViewById(R.id.item_audit_state)
        val  carAudit : TextView = view.findViewById(R.id.item_audit)
        val  carPlane: TextView = view.findViewById(R.id.item_audit_plane)
        val  carName: TextView = view.findViewById(R.id.item_audit_name)
        val  carPurpose: TextView = view.findViewById(R.id.item_audit_purpose)
        val  carTime: TextView = view.findViewById(R.id.item_audit_time)
        val  carYes: Button = view.findViewById(R.id.item_audit_yes)
        val  carNo: Button = view.findViewById(R.id.item_audit_no)
    }
    class VisitViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val visitState: TextView = view.findViewById(R.id.item_audit_state)
        val visitAudit : TextView = view.findViewById(R.id.item_audit)
        val visitPlane: TextView = view.findViewById(R.id.item_audit_plane)
        val visitName: TextView = view.findViewById(R.id.item_audit_name)
        val visitPurpose: TextView = view.findViewById(R.id.item_audit_purpose)
        val visitTime: TextView = view.findViewById(R.id.item_audit_time)
        val visitYes: Button = view.findViewById(R.id.item_audit_yes)
        val visitNo: Button = view.findViewById(R.id.item_audit_no)
    }
    class NewsViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val newsState: TextView = view.findViewById(R.id.item_audit_state)
        val newsAudit : TextView = view.findViewById(R.id.item_audit)
        val newsPlane: TextView = view.findViewById(R.id.item_audit_plane)
        val newsName: TextView = view.findViewById(R.id.item_audit_name)
        val newsPurpose: TextView = view.findViewById(R.id.item_audit_purpose)
        val newsTime: TextView = view.findViewById(R.id.item_audit_time)
        val newsYes: Button = view.findViewById(R.id.item_audit_yes)
        val newsNo: Button = view.findViewById(R.id.item_audit_no)
    }
    fun setupLayoutManager(recyclerView: RecyclerView) {
        val layoutManager = LinearLayoutManager(recyclerView.context)
        recyclerView.layoutManager = layoutManager
//        // 添加间距装饰（水平间距16dp，垂直间距8dp）
        val spacing = recyclerView.context.resources.getDimensionPixelSize(R.dimen.grid_spacing)
        recyclerView.addItemDecoration(GridSpaceItemDecoration(
            horizontalSpacing = spacing,  // 水平间距（左右）
            verticalSpacing = spacing / 2 // 垂直间距（上下，可根据需求调整）
        ))
        // 可选：启用预加载（提升滑动流畅性）
        recyclerView.setItemViewCacheSize(20)
    }
}